package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.constant.AuthConstant;
import www.dashu.com.domain.SysUser;
import www.dashu.com.domain.SysUserApp;
import www.dashu.com.domain.SysUserRole;
import www.dashu.com.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 后台用户管理
 */
@Controller
@Api(tags = "UmsAdminController", description = "登陆中转")
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    SysUserService userService;
    @Autowired
    SysUserRoleService userRoleService;
    @Autowired
    SysUserAppService userAppService;
    @Autowired
    AuthService authService;
    @Autowired
    AdminService adminService;

    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@Validated @RequestBody SysUser sysUser) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("client_id", AuthConstant.ADMIN_CLIENT_ID);
        parameters.put("client_secret","123456");
        parameters.put("grant_type","password");
        parameters.put("username",sysUser.getUsername());
        parameters.put("password",sysUser.getPassword());
        return authService.getAccessToken(parameters);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAdminInfo() {
        SysUser umsAdmin = adminService.getCurrentAdmin();
        Map<String, Object> data = new HashMap<>();
        data.put("username", umsAdmin.getUsername());
//        data.put("menus", roleService.getMenuList(umsAdmin.getId()));
        data.put("icon", umsAdmin.getIcon());
        List<SysUserRole> userRoles = userRoleService.findUserRolesByUserId(umsAdmin.getId());
        List<String> roles = userRoles.stream().map(SysUserRole::getRoleName).collect(Collectors.toList());
        List<Long> rids = userRoles.stream().map(SysUserRole::getRId).collect(Collectors.toList());
        data.put("roles",roles);
        data.put("rids",rids);
        return CommonResult.success(data);
    }

    @ApiOperation("根据用户名获取通用用户信息")
    @RequestMapping(value = "/loadByUsername", method = RequestMethod.GET)
    @ResponseBody
    public SysUser loadUserByUsername(@RequestParam String username) {
        return findUserDetail(username);
    }

    private SysUser findUserDetail(String username)
    {
        SysUser sysUser = userService.findByUsername(username);
        if (null != sysUser) {
            List<SysUserRole> userRoles = userRoleService.findUserRolesByUserId(sysUser.getId());
            List<String> roleStrList = userRoles.stream().map(item -> item.getRId() + "_" + item.getRoleName()).collect(Collectors.toList());
            sysUser.setRoles(roleStrList);

            List<SysUserApp> userApps = userAppService.findUserAppsByUserId(sysUser.getId());
            List<String> appLists = userApps.stream().map(item -> item.getAppApi()).collect(Collectors.toList());
            sysUser.setAppApis(appLists);
        }
        return sysUser;
    }

    @ApiOperation(value = "登出功能")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult logout() {
        return CommonResult.success(null);
    }

}
